/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormRPembelian.java
 *
 * Created on Feb 2, 2011, 4:45:49 PM
 */

package juwita;

import config.DBConnection;
import java.awt.CardLayout;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.swing.table.DefaultTableModel;
import javax.swing.JOptionPane;
import javax.swing.table.TableColumn;


/**
 *
 * @author Abah
 */
public class FormRPembelian extends javax.swing.JInternalFrame {

    private DefaultTableModel DftTabModeReturBeli;
    private DefaultTableModel DftTabModeBeli;
    java.sql.Connection conn = new DBConnection().connect();

    String sTglRBeli = "";
    
    /** Creates new form FormRPembelian */
    public FormRPembelian() {
        initComponents();
        
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        tbReturBeli.getTableHeader().setReorderingAllowed(false);
        tbBeli.getTableHeader().setReorderingAllowed(false);
        setKdReturBeli();

        tampilDataReturBeli();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnTengah = new javax.swing.JPanel();
        pnAtas = new javax.swing.JPanel();
        btnDelete = new javax.swing.JButton();
        txtKdBeli = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtKdReturBeli = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        btnNew = new javax.swing.JButton();
        txtNmSupplier = new javax.swing.JTextField();
        btnEdit = new javax.swing.JButton();
        btnCariBeli = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        spDetailBeli = new javax.swing.JScrollPane();
        tbDetailBeli = new javax.swing.JTable();
        dcTglReturBeli = new com.toedter.calendar.JDateChooser();
        jLabel9 = new javax.swing.JLabel();
        txtKdSupplier = new javax.swing.JTextField();
        pnShow = new javax.swing.JPanel();
        pnReturBeli = new javax.swing.JPanel();
        spReturBeli = new javax.swing.JScrollPane();
        tbReturBeli = new javax.swing.JTable();
        pnSearchReturBeli = new javax.swing.JPanel();
        btnReturSearch = new javax.swing.JButton();
        btnReturShowAll = new javax.swing.JButton();
        txtSrcReturBeli = new javax.swing.JTextField();
        cbSrcReturBeli = new javax.swing.JComboBox();
        infoReturBeli = new javax.swing.JLabel();
        pnBeli = new javax.swing.JPanel();
        pnCrBeli = new javax.swing.JPanel();
        spBeli = new javax.swing.JScrollPane();
        tbBeli = new javax.swing.JTable();
        pnAtas1 = new javax.swing.JPanel();
        btnShowAllBeli = new javax.swing.JButton();
        txtSrcBeli = new javax.swing.JTextField();
        cbSrcBeli = new javax.swing.JComboBox();
        btnSearchBeli = new javax.swing.JButton();
        lblCloseBeli = new javax.swing.JLabel();
        infoBeli = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Retur Pembelian");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/returbeli.png"))); // NOI18N
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        pnTengah.setBackground(new java.awt.Color(255, 255, 255));
        pnTengah.setLayout(new java.awt.BorderLayout());

        pnAtas.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 255), 2));
        pnAtas.setPreferredSize(new java.awt.Dimension(410, 230));
        pnAtas.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnDelete.setText("Delete");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });
        pnAtas.add(btnDelete, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 80, 70, 20));

        txtKdBeli.setEditable(false);
        txtKdBeli.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnAtas.add(txtKdBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 40, 110, -1));

        jLabel2.setText("Kode Beli");
        pnAtas.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        txtKdReturBeli.setEditable(false);
        txtKdReturBeli.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtKdReturBeli.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtKdReturBeliKeyReleased(evt);
            }
        });
        pnAtas.add(txtKdReturBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 10, 110, -1));

        jLabel3.setText("Kode Retur Beli");
        pnAtas.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        btnNew.setText("New");
        btnNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNewActionPerformed(evt);
            }
        });
        pnAtas.add(btnNew, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 20, 70, -1));

        txtNmSupplier.setEditable(false);
        pnAtas.add(txtNmSupplier, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 80, 240, -1));

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });
        pnAtas.add(btnEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 50, 70, 20));

        btnCariBeli.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/search.png"))); // NOI18N
        btnCariBeli.setText("cari");
        btnCariBeli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCariBeliActionPerformed(evt);
            }
        });
        pnAtas.add(btnCariBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 40, 70, 20));

        jLabel8.setText("Tanggal");
        pnAtas.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 100, -1, -1));

        tbDetailBeli.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDetailBeli.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tbDetailBeli.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbDetailBeliMousePressed(evt);
            }
        });
        tbDetailBeli.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbDetailBeliKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbDetailBeliKeyReleased(evt);
            }
        });
        spDetailBeli.setViewportView(tbDetailBeli);

        pnAtas.add(spDetailBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 130, 440, 100));

        dcTglReturBeli.setDateFormatString("dd/MM/yyyy");
        dcTglReturBeli.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dcTglReturBeliPropertyChange(evt);
            }
        });
        pnAtas.add(dcTglReturBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 100, 140, -1));

        jLabel9.setText("Supplier");
        pnAtas.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 60, -1, -1));

        txtKdSupplier.setEditable(false);
        txtKdSupplier.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnAtas.add(txtKdSupplier, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 60, 70, -1));

        pnTengah.add(pnAtas, java.awt.BorderLayout.PAGE_START);

        pnShow.setLayout(new java.awt.CardLayout());

        pnReturBeli.setLayout(new java.awt.BorderLayout());

        spReturBeli.setAutoscrolls(true);

        tbReturBeli.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbReturBeli.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tbReturBeli.setGridColor(new java.awt.Color(0, 0, 255));
        tbReturBeli.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbReturBeliMousePressed(evt);
            }
        });
        tbReturBeli.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbReturBeliKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbReturBeliKeyReleased(evt);
            }
        });
        spReturBeli.setViewportView(tbReturBeli);

        pnReturBeli.add(spReturBeli, java.awt.BorderLayout.CENTER);

        pnSearchReturBeli.setBackground(new java.awt.Color(255, 255, 255));
        pnSearchReturBeli.setPreferredSize(new java.awt.Dimension(0, 70));
        pnSearchReturBeli.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnReturSearch.setText("Search");
        btnReturSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReturSearchActionPerformed(evt);
            }
        });
        pnSearchReturBeli.add(btnReturSearch, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 40, 80, -1));

        btnReturShowAll.setText("Show All");
        btnReturShowAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReturShowAllActionPerformed(evt);
            }
        });
        pnSearchReturBeli.add(btnReturShowAll, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 10, 80, -1));
        pnSearchReturBeli.add(txtSrcReturBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 40, 210, 20));

        cbSrcReturBeli.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kode Retur", "Kode Beli", "Supplier" }));
        pnSearchReturBeli.add(cbSrcReturBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, 100, 20));

        infoReturBeli.setFont(new java.awt.Font("Tahoma", 1, 14));
        infoReturBeli.setForeground(new java.awt.Color(0, 0, 204));
        infoReturBeli.setText("Search Data Retur Pembelian");
        pnSearchReturBeli.add(infoReturBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, 20));

        pnReturBeli.add(pnSearchReturBeli, java.awt.BorderLayout.PAGE_START);

        pnShow.add(pnReturBeli, "returbeli");

        pnBeli.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 204)));
        pnBeli.setLayout(new java.awt.BorderLayout());

        pnCrBeli.setBackground(new java.awt.Color(255, 255, 255));
        pnCrBeli.setLayout(new java.awt.BorderLayout());

        tbBeli.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbBeli.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        tbBeli.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tbBeliMousePressed(evt);
            }
        });
        tbBeli.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tbBeliKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tbBeliKeyReleased(evt);
            }
        });
        spBeli.setViewportView(tbBeli);

        pnCrBeli.add(spBeli, java.awt.BorderLayout.CENTER);

        pnAtas1.setBackground(new java.awt.Color(255, 255, 255));
        pnAtas1.setPreferredSize(new java.awt.Dimension(410, 70));
        pnAtas1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        btnShowAllBeli.setText("Show All");
        btnShowAllBeli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnShowAllBeliActionPerformed(evt);
            }
        });
        pnAtas1.add(btnShowAllBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 40, 80, -1));
        pnAtas1.add(txtSrcBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 270, -1));

        cbSrcBeli.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Kode Beli", "Supplier" }));
        pnAtas1.add(cbSrcBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, 100, -1));

        btnSearchBeli.setText("Search");
        btnSearchBeli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchBeliActionPerformed(evt);
            }
        });
        pnAtas1.add(btnSearchBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 10, 80, -1));

        lblCloseBeli.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resource/Delete.png"))); // NOI18N
        lblCloseBeli.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lblCloseBeliMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                lblCloseBeliMouseReleased(evt);
            }
        });
        pnAtas1.add(lblCloseBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 20, 40, 40));

        infoBeli.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        infoBeli.setForeground(new java.awt.Color(0, 0, 204));
        infoBeli.setText("Daftar Pembelian");
        pnAtas1.add(infoBeli, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 10, -1, -1));

        pnCrBeli.add(pnAtas1, java.awt.BorderLayout.PAGE_START);

        pnBeli.add(pnCrBeli, java.awt.BorderLayout.CENTER);

        pnShow.add(pnBeli, "beli");

        pnTengah.add(pnShow, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 443, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(pnTengah, javax.swing.GroupLayout.PREFERRED_SIZE, 443, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 427, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(pnTengah, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
        int clossing;
        clossing = JOptionPane.showConfirmDialog(this,
                "Tutup Retur Pembelian?",
                "Konfirmasi",
                JOptionPane.YES_NO_OPTION);
        if (clossing == 0){
            this.dispose();
            FormMain.isfrPem = false;
        }
    }//GEN-LAST:event_formInternalFrameClosing

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if(btnDelete.getText().equals("Cancel")){
            kosongkan();
            tampilDataReturBeli();
        }else if(btnDelete.getText().equals("Delete")){
            deleteData();
        }

        //*
        //*/
}//GEN-LAST:event_btnDeleteActionPerformed

    private void txtKdReturBeliKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtKdReturBeliKeyReleased
        // TODO add your handling code here:
}//GEN-LAST:event_txtKdReturBeliKeyReleased

    private void btnNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNewActionPerformed
        // TODO add your handling code here:
        kosongkanNew();
}//GEN-LAST:event_btnNewActionPerformed

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEditActionPerformed
        // TODO add your handling code here:
        if(btnEdit.getText().equals("Save")){
            if(txtKdBeli.getText().equals("")){
                JOptionPane.showMessageDialog(this, "Username harus diisi!",
                        "Informasi",JOptionPane.INFORMATION_MESSAGE);
            }else{
                insertData();
            }
        }else if (btnEdit.getText().equals("Edit")){
            editData();
        }
}//GEN-LAST:event_btnEditActionPerformed

    private void btnCariBeliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCariBeliActionPerformed
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(pnShow.getLayout());
        cl.show(pnShow, "beli");
        tampilSearchBeli();
        kosongkanNew();
}//GEN-LAST:event_btnCariBeliActionPerformed

    private void tbReturBeliMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbReturBeliMousePressed
        getDataFromReturBeli();
}//GEN-LAST:event_tbReturBeliMousePressed

    private void tbReturBeliKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbReturBeliKeyPressed
        // TODO add your handling code here:
        getDataFromReturBeli();
}//GEN-LAST:event_tbReturBeliKeyPressed

    private void tbReturBeliKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbReturBeliKeyReleased
        // TODO add your handling code here:
        getDataFromReturBeli();
}//GEN-LAST:event_tbReturBeliKeyReleased

    private void btnReturSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReturSearchActionPerformed
        // TODO add your handling code here:
        tampilSearchReturBeli();
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
}//GEN-LAST:event_btnReturSearchActionPerformed

    private void btnReturShowAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReturShowAllActionPerformed
        // TODO add your handling code here:
        tampilDataReturBeli();
}//GEN-LAST:event_btnReturShowAllActionPerformed

    private void tbBeliMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbBeliMousePressed
        getDataFromBeli();
}//GEN-LAST:event_tbBeliMousePressed

    private void tbBeliKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBeliKeyPressed
        // TODO add your handling code here:
        getDataFromBeli();
}//GEN-LAST:event_tbBeliKeyPressed

    private void tbBeliKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbBeliKeyReleased
        // TODO add your handling code here:
        getDataFromBeli();
}//GEN-LAST:event_tbBeliKeyReleased

    private void btnShowAllBeliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowAllBeliActionPerformed
        // TODO add your handling code here:
        tampilAllBeli();
}//GEN-LAST:event_btnShowAllBeliActionPerformed

    private void btnSearchBeliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchBeliActionPerformed
        // TODO add your handling code here:
        tampilSearchBeli();
}//GEN-LAST:event_btnSearchBeliActionPerformed

    private void lblCloseBeliMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblCloseBeliMouseClicked
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(pnShow.getLayout());
        cl.show(pnShow, "returbeli");
}//GEN-LAST:event_lblCloseBeliMouseClicked

    private void lblCloseBeliMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lblCloseBeliMouseReleased
        // TODO add your handling code here:
        CardLayout cl = (CardLayout)(pnShow.getLayout());
        cl.show(pnShow, "returbeli");
}//GEN-LAST:event_lblCloseBeliMouseReleased

    private void tbDetailBeliMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDetailBeliMousePressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tbDetailBeliMousePressed

    private void tbDetailBeliKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbDetailBeliKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_tbDetailBeliKeyPressed

    private void tbDetailBeliKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbDetailBeliKeyReleased
        // TODO add your handling code here:
    }//GEN-LAST:event_tbDetailBeliKeyReleased

    private void dcTglReturBeliPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dcTglReturBeliPropertyChange
        // TODO add your handling code here:
        if(dcTglReturBeli.getDate()!=null){
            //format yyyy-MM-dd utk mysql
            SimpleDateFormat formatTgl = new SimpleDateFormat("yyyy-MM-dd");
            sTglRBeli= formatTgl.format(dcTglReturBeli.getDate());
        }
}//GEN-LAST:event_dcTglReturBeliPropertyChange


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCariBeli;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnNew;
    private javax.swing.JButton btnReturSearch;
    private javax.swing.JButton btnReturShowAll;
    private javax.swing.JButton btnSearchBeli;
    private javax.swing.JButton btnShowAllBeli;
    private javax.swing.JComboBox cbSrcBeli;
    private javax.swing.JComboBox cbSrcReturBeli;
    private com.toedter.calendar.JDateChooser dcTglReturBeli;
    private javax.swing.JLabel infoBeli;
    private javax.swing.JLabel infoReturBeli;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel lblCloseBeli;
    private javax.swing.JPanel pnAtas;
    private javax.swing.JPanel pnAtas1;
    private javax.swing.JPanel pnBeli;
    private javax.swing.JPanel pnCrBeli;
    private javax.swing.JPanel pnReturBeli;
    private javax.swing.JPanel pnSearchReturBeli;
    private javax.swing.JPanel pnShow;
    private javax.swing.JPanel pnTengah;
    private javax.swing.JScrollPane spBeli;
    private javax.swing.JScrollPane spDetailBeli;
    private javax.swing.JScrollPane spReturBeli;
    private javax.swing.JTable tbBeli;
    private javax.swing.JTable tbDetailBeli;
    private javax.swing.JTable tbReturBeli;
    private javax.swing.JTextField txtKdBeli;
    private javax.swing.JTextField txtKdReturBeli;
    private javax.swing.JTextField txtKdSupplier;
    private javax.swing.JTextField txtNmSupplier;
    private javax.swing.JTextField txtSrcBeli;
    private javax.swing.JTextField txtSrcReturBeli;
    // End of variables declaration//GEN-END:variables

    private void kosongkan(){
        txtKdBeli.setText("");
        txtNmSupplier.setText("");
        txtSrcReturBeli.setText("");
        btnEdit.setEnabled(false);
        btnDelete.setEnabled(false);
        btnEdit.setText("Edit");
        btnDelete.setText("Delete");
        
    }
    private void kosongkanNew(){
        setKdReturBeli();
        txtKdBeli.setText("");
        txtNmSupplier.setText("");
        txtSrcReturBeli.setText("");
        btnEdit.setText("Save");
        btnDelete.setText("Cancel");
        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);        
    }
    private void tampilDataReturBeli(){
        Object[] Baris={"Kode Retur", "Kode Beli", "Supplier", "Tanggal"};
        DftTabModeReturBeli=new DefaultTableModel(null,Baris);
        tbReturBeli.setModel(DftTabModeReturBeli);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("SELECT br.*, s.nmSup " +
                    "FROM tbeli_retur br, tsup s " +
                    "WHERE br.kdSup = s.kdSup " +
                    "ORDER BY idReturBeli");
            while (rslt.next()) {
                String sKdRBeli = rslt.getString("idReturBeli");
                String sKdBeli = rslt.getString("idBeli");
                String sNmSup = rslt.getString("nmSup");
                //Date dRBeli = rslt.getDate("tgl_retur_beli");
                //SimpleDateFormat formatTgl = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat formatTgl = new SimpleDateFormat("dd-MM-yyyy");
                String sTgl= formatTgl.format(rslt.getDate("tgl_retur_beli"));

                String[] dataField = {sKdRBeli, sKdBeli, sNmSup, sTgl};
                DftTabModeReturBeli.addRow(dataField);
                int widths[] = {100, 100, 200, 100};
                for(int i=0;i<tbReturBeli.getColumnCount();i++){
                    TableColumn column = tbReturBeli.getColumnModel().getColumn(i);
                    column.setPreferredWidth(widths[i]);
                }
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void tampilSearchReturBeli(){
        String srcby = "";
        if(cbSrcReturBeli.getSelectedItem().toString().equals("Kode Retur")){
            srcby = "idReturBeli";
        }else if(cbSrcReturBeli.getSelectedItem().toString().equals("Kode Beli")){
            srcby = "idBeli";
        }else if(cbSrcReturBeli.getSelectedItem().toString().equals("Supplier")){
            srcby = "nmSup";
        }

        Object[] Baris={"Kode Retur","Kode Beli", "Supplier", "Tanggal"};
        DftTabModeReturBeli=new DefaultTableModel(null,Baris);
        tbReturBeli.setModel(DftTabModeReturBeli);
        
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT br.*, s.nmSup " +
                    "FROM tbeli_retur br, tsup s " +
                    "WHERE br.kdSup = s.kdSup " +
                    "AND UPPER(" + srcby +
                    ") like UPPER('%" +
                    txtSrcReturBeli.getText() + "%') ORDER BY idReturBeli");
            while (rslt.next()) {
                String sKdRBeli = rslt.getString("idReturBeli");
                String sKdBeli = rslt.getString("idBeli");
                String sNmSup = rslt.getString("nmSup");
                SimpleDateFormat formatTgl = new SimpleDateFormat("dd-MM-yyyy");
                String sTgl= formatTgl.format(rslt.getDate("tgl_retur_beli"));
                
                String[] dataField = {sKdRBeli, sKdBeli, sNmSup, sTgl};
                DftTabModeReturBeli.addRow(dataField);
                int widths[] = {100,100, 200, 100};
                for(int i=0;i<tbReturBeli.getColumnCount();i++){
                    TableColumn column = tbReturBeli.getColumnModel().getColumn(i);
                    column.setPreferredWidth(widths[i]);
                }
            }

        }
        catch(SQLException e) {
            System.out.println("gagal! : " + e.toString());
        }
    }

    private void tampilSearchBeli(){
        String srcby = "";
        if(cbSrcBeli.getSelectedItem().toString().equals("Kode Beli")){
            srcby = "idBeli";
        }else{
            srcby = "nmSup";
        }

        Object[] Baris={"Kode Beli","Supplier", "Tanggal"};
        DftTabModeBeli=new DefaultTableModel(null,Baris);
        tbBeli.setModel(DftTabModeBeli);
        int widths[] = {100, 200, 100};
        for(int i=0;i<tbBeli.getColumnCount();i++){
            TableColumn column = tbBeli.getColumnModel().getColumn(i);
            column.setPreferredWidth(widths[i]);
        }
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT b.*, s.nmSup FROM tbeli b, tsup s " +
                    "WHERE b.kdSup=s.kdSup AND " +
                    "UPPER(" + srcby +
                    ") like UPPER('%" +
                    txtSrcBeli.getText() + "%')");
            while (rslt.next()) {
                String sIdBeli = rslt.getString("idBeli");
                String sNmSup = rslt.getString("nmSup");

                String[] dataField = {sIdBeli, sNmSup};
                DftTabModeBeli.addRow(dataField);
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
    }

    private void deleteData(){
            try {
                int deleting;
                deleting = JOptionPane.showConfirmDialog(this,
                        "Apakah Anda yakin akan menghapus "

                        //+ cbKodeKota.getSelectedItem().toString() +" "
                        +txtKdReturBeli.getText() +"?",
                        "Konfirmasi",
                        JOptionPane.YES_NO_OPTION);
                if (deleting == 0){

                    PreparedStatement pStatement = null;
                    String sql = "DELETE FROM tbeli_retur " +
                            "WHERE idReturBeli=? ";
                    pStatement = conn.prepareStatement(sql);

                    pStatement.setString(1, txtKdReturBeli.getText());

                    int intTambah = pStatement.executeUpdate();
                    if (intTambah >0){
                        JOptionPane.showMessageDialog(this,
                                "Delete sukses", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);

                    }else {
                        JOptionPane.showMessageDialog(this,
                                "Delete gagal", "Informasi",
                                JOptionPane.INFORMATION_MESSAGE);
                    }

                    pStatement.close();

                    kosongkan();
                    tampilDataReturBeli();
                }
            } catch(SQLException e){
                System.out.println("koneksi gagal " + e.toString());
            }
    }
    private boolean isDuplicatePKey(){
        String pKey = txtKdReturBeli.getText();
        boolean result = true;
        Integer row = 0;

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("Select * from tbeli_retur " +
                    "WHERE idReturBeli=" +
                    "'" +pKey+ "'");
            while (rslt.next()) {
                row = rslt.getRow();
            }
                if(row > 0){
                    //JOptionPane.showMessageDialog(this, "Kode "+pKey+" sudah ada"
                    //        ,"Peringatan",JOptionPane.WARNING_MESSAGE);
                    result = true;
                }else{
                    result = false;
                }
            System.out.println("row : " + row);
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
        return result;
    }
    private void insertData(){
        java.sql.PreparedStatement pStatement = null;
        String sql = "INSERT INTO tbeli_retur " +
                "(idReturBeli, idBeli, tgl_retur_beli, kdSup, user_id) " +
                "VALUES (?,?,?, ?,?)";
        try {
            pStatement = conn.prepareStatement(sql);
            pStatement.setString(1, txtKdReturBeli.getText());
            pStatement.setString(2, txtKdBeli.getText());
            //for date
            pStatement.setString(3, sTglRBeli);
            pStatement.setString(4, txtKdSupplier.getText());
            pStatement.setString(5, "admin");
            
            int intTambah = pStatement.executeUpdate();
            if (intTambah>0){
                JOptionPane.showMessageDialog(this,
                        "Penambahan sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this,
                        "Penambahan gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            pStatement.close();
            
            kosongkan();
            tampilDataReturBeli();
            setKdReturBeli();

        }
        catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }

    }

     private void editData(){        
        try {
            PreparedStatement pStatement = null;
            String sql = "UPDATE tbeli_retur " +
                    "SET idBeli=?, tgl_retur_beli=?, " +
                    "kdSup=? " +
                    "WHERE idReturBeli=? ";
            pStatement = conn.prepareStatement(sql);
            
            pStatement.setString(1, txtKdBeli.getText());
            pStatement.setString(2, sTglRBeli);
            pStatement.setString(3, txtKdSupplier.getText());
            pStatement.setString(4, txtKdReturBeli.getText());
            
            int intTambah = pStatement.executeUpdate();
            if (intTambah >0){
                JOptionPane.showMessageDialog(this,
                        "Edit sukses", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);

            }else {
                JOptionPane.showMessageDialog(this,
                        "Edit gagal", "Informasi",
                        JOptionPane.INFORMATION_MESSAGE);
            }
            
            pStatement.close();
            kosongkan();
            tampilDataReturBeli();
            
        } catch(SQLException e){
            System.out.println("koneksi gagal " + e.toString());
        }
    }
    private void getDataFromReturBeli(){        
        Object oIdRBeli, oIdBeli;

        oIdRBeli = tbReturBeli.getValueAt(tbReturBeli.getSelectedRow(), 0);
        oIdBeli = tbReturBeli.getValueAt(tbReturBeli.getSelectedRow(), 1);
        
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery("SELECT br.*, s.nmSup " +
                    "FROM tbeli_retur br, tsup s " +
                    "WHERE br.kdSup = s.kdSup " +
                    "AND idReturBeli=" +
                    "'" +oIdRBeli+ "'");
            while (rslt.next()) {
                txtKdSupplier.setText(rslt.getString("kdSup"));
                txtNmSupplier.setText(rslt.getString("nmSup"));
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        txtKdReturBeli.setText(oIdRBeli.toString());
        txtKdBeli.setText(oIdBeli.toString());

        btnEdit.setEnabled(true);
        btnDelete.setEnabled(true);
        btnEdit.setText("Edit");
        btnDelete.setText("Delete");
    }

    private void setKdReturBeli(){
        String idReturBeli = "";
        Integer noUrut = 0;
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT * FROM tbeli_retur");            
            while (rslt.next()) {
                noUrut = rslt.getRow();
                idReturBeli = rslt.getString("idReturBeli");
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }
                
        if(idReturBeli.equals("")){
            idReturBeli = "RB-0000000";
            idReturBeli = idReturBeli.substring(3);
            noUrut = Integer.parseInt(idReturBeli);
        }
        System.out.println(idReturBeli);

        noUrut++;

        java.text.DecimalFormat nft = new java.text.DecimalFormat("#0000000.###");
        nft.setDecimalSeparatorAlwaysShown(false);

        txtKdReturBeli.setText("RB-"+nft.format(noUrut));
        while(isDuplicatePKey()){
            noUrut++;
            txtKdReturBeli.setText("RB-"+nft.format(noUrut));
        }
        System.out.println("RB-"+nft.format(noUrut));
    }

    private void getDataFromBeli(){
                // TODO add your handling code here:
        Object oIdBeli, oKdSup;//, oKet;//, oTest;

        oIdBeli = tbBeli.getValueAt(tbBeli.getSelectedRow(), 0);
        oKdSup = tbBeli.getValueAt(tbBeli.getSelectedRow(), 1);
        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT b.*, s.nmSup " +
                    "FROM tbeli b, tsup s WHERE " +
                    "b.kdSup = s.kdSup " +
                    "AND idBeli=" +
                    "'" +oIdBeli+ "'");
            while (rslt.next()) {
                txtNmSupplier.setText(rslt.getString("nmSup"));                          
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

        txtKdBeli.setText(oIdBeli.toString());
        txtKdSupplier.setText(oKdSup.toString());        
    }

    private void tampilAllBeli(){
        txtSrcBeli.setText("");
        Object[] Baris={"Kode Beli","Supplier","Tanggal"};
        DftTabModeBeli=new DefaultTableModel(null,Baris);
        tbBeli.setModel(DftTabModeBeli);

        try {
            java.sql.Statement stmt = conn.createStatement();
            java.sql.ResultSet rslt = stmt.executeQuery(
                    "SELECT b.*, s.nmSup FROM tbeli b, tsup s " +
                    "WHERE b.kdSup=s.kdSup " +
                    "ORDER BY idBeli");
            while (rslt.next()) {
                String sIdBeli = rslt.getString("idBeli");                
                String sNmSup = rslt.getString("nmSup");
                SimpleDateFormat formatTgl = new SimpleDateFormat("dd-MM-yyyy");
                String sTgl= formatTgl.format(rslt.getDate("tgl_beli"));
                
                String[] dataField = {sIdBeli, sNmSup, sTgl};
                DftTabModeBeli.addRow(dataField);
                int widths[] = {120, 300};
                for(int i=0;i<tbBeli.getColumnCount();i++){
                    TableColumn column = tbBeli.getColumnModel().getColumn(i);
                    column.setPreferredWidth(widths[i]);
                }
            }
        }
        catch(SQLException e) {
            System.out.println("gagal : " + e.toString());
        }

    }    
}
